﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:DetectMode
	/// </summary>
	public partial class DetectMode
	{
		public DetectMode()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SQLiteHelper.GetMaxID("ID", "DetectMode"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from DetectMode");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Byte,1)			};
			parameters[0].Value = ID;

			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.DetectMode model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into DetectMode(");
			strSql.Append("ID,modeName,readyDetect,stripAmount,cupAmount,CurrentDetectItem)");
			strSql.Append(" values (");
			strSql.Append("@ID,@modeName,@readyDetect,@stripAmount,@cupAmount,@CurrentDetectItem)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Byte,1),
					new SQLiteParameter("@modeName", DbType.String,15),
					new SQLiteParameter("@readyDetect", DbType.Int32,4),
					new SQLiteParameter("@stripAmount", DbType.Int32,4),
					new SQLiteParameter("@cupAmount", DbType.Int32,4),
					new SQLiteParameter("@CurrentDetectItem", DbType.String,20)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.modeName;
			parameters[2].Value = model.readyDetect;
			parameters[3].Value = model.stripAmount;
			parameters[4].Value = model.cupAmount;
			parameters[5].Value = model.CurrentDetectItem;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.DetectMode model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update DetectMode set ");
			strSql.Append("modeName=@modeName,");
			strSql.Append("readyDetect=@readyDetect,");
			strSql.Append("stripAmount=@stripAmount,");
			strSql.Append("cupAmount=@cupAmount,");
			strSql.Append("CurrentDetectItem=@CurrentDetectItem");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@modeName", DbType.String,15),
					new SQLiteParameter("@readyDetect", DbType.Int32,4),
					new SQLiteParameter("@stripAmount", DbType.Int32,4),
					new SQLiteParameter("@cupAmount", DbType.Int32,4),
					new SQLiteParameter("@CurrentDetectItem", DbType.String,20),
					new SQLiteParameter("@ID", DbType.Byte,1)};
			parameters[0].Value = model.modeName;
			parameters[1].Value = model.readyDetect;
			parameters[2].Value = model.stripAmount;
			parameters[3].Value = model.cupAmount;
			parameters[4].Value = model.CurrentDetectItem;
			parameters[5].Value = model.ID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DetectMode ");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Byte,1)			};
			parameters[0].Value = ID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DetectMode ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectMode GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,modeName,readyDetect,stripAmount,cupAmount,CurrentDetectItem from DetectMode ");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Byte,1)			};
			parameters[0].Value = ID;

			Clipper.Model.DetectMode model=new Clipper.Model.DetectMode();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectMode DataRowToModel(DataRow row)
		{
			Clipper.Model.DetectMode model=new Clipper.Model.DetectMode();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["modeName"]!=null)
				{
					model.modeName=row["modeName"].ToString();
				}
				if(row["readyDetect"]!=null && row["readyDetect"].ToString()!="")
				{
					model.readyDetect=int.Parse(row["readyDetect"].ToString());
				}
				if(row["stripAmount"]!=null && row["stripAmount"].ToString()!="")
				{
					model.stripAmount=int.Parse(row["stripAmount"].ToString());
				}
				if(row["cupAmount"]!=null && row["cupAmount"].ToString()!="")
				{
					model.cupAmount=int.Parse(row["cupAmount"].ToString());
				}
				if(row["CurrentDetectItem"]!=null)
				{
					model.CurrentDetectItem=row["CurrentDetectItem"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,modeName,readyDetect,stripAmount,cupAmount,CurrentDetectItem ");
			strSql.Append(" FROM DetectMode ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM DetectMode ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from DetectMode T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "DetectMode";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

